$(window).on('load', function() {
    let box = $('#box');
    let searchName = $('#searchBar').val();
    let c2 = false;
    let c3 = false;
    let orderType = 0;
    let scon = 0;
    
    // 初始化加载商品
    goodsFind();
    
    // 事件监听器保持不变...
    
    function goodsFind() {
        box.empty().append('<div class="text-center py-5"><i class="fas fa-spinner fa-spin fa-2x"></i><p>正在加载商品...</p></div>');
        
        let con = {
            searchName: searchName,
            pageNum: 1,
            pageSize: 30,
            orderType: orderType,
            con2: c2,
            con3: c3,
            scon: scon
        };
        
        $.ajax({
            url: "searchGoodsDo",
            type: 'POST',
            contentType: "application/json",
            data: JSON.stringify(con),
            dataType: 'json',
            success: function(data) {
                box.empty();
                
                if (data.length === 0) {
                    box.html(`
                        <div class="text-center py-5">
                            <i class="far fa-frown fa-3x mb-3" style="color: #ccc;"></i>
                            <h4>没有找到相关商品</h4>
                            <p class="text-muted">尝试调整搜索条件或关键词</p>
                        </div>
                    `);
                    return;
                }
                
                let row = $('<div class="row"></div>');
                
                $.each(data, function(index, item) {
                    const card = $(`
                        <div class="col-lg-2 col-md-4 col-sm-6 mb-4">
                            <div class="goods-card">
                                <form action="goodsInfo" class="goodsInfoForm">
                                    <input type="hidden" name="goodId" value="${item.goodslist.goodid}">
                                    <input type="hidden" name="userId" value="${item.user.userid}">
                                    <a href="#" onclick="$(this).closest('form').submit();" class="text-decoration-none">
                                        <div class="goods-img-container">
                                            <img src="${item.goodslist.goodimg || 'img/default-product.png'}" 
                                                 alt="${item.goodslist.goodname}" 
                                                 class="goods-img"
                                                 onerror="this.src='img/default-product.png'">
                                        </div>
                                        <div class="goods-info">
                                            <h3 class="goods-title">${item.goodslist.goodname}</h3>
                                            <div class="goods-price">￥${item.goodslist.goodprice}</div>
                                            <p class="goods-desc">${item.goodslist.gooddec || '暂无描述'}</p>
                                            <div class="seller-info">
                                                <img src="${item.user.headimg || 'img/headimg_def.jpg'}" 
                                                     alt="${item.user.username}" 
                                                     class="seller-avatar">
                                                <span class="seller-name">${item.user.username}</span>
                                            </div>
                                        </div>
                                    </a>
                                </form>
                            </div>
                        </div>
                    `);
                    
                    row.append(card);
                    
                    // 每行6个商品后换行
                    if ((index + 1) % 6 === 0 && index + 1 !== data.length) {
                        box.append(row);
                        row = $('<div class="row"></div>');
                    }
                });
                
                // 添加剩余的商品
                if (row.children().length > 0) {
                    box.append(row);
                }
            },
            error: function(xhr, status, error) {
                box.empty().html(`
                    <div class="text-center py-5">
                        <i class="fas fa-exclamation-triangle fa-3x mb-3 text-danger"></i>
                        <h4>加载失败</h4>
                        <p class="text-muted">请稍后再试或刷新页面</p>
                        <button class="btn btn-primary mt-3" onclick="location.reload()">重新加载</button>
                    </div>
                `);
                console.error('Error:', status, error);
            }
        });
    }
});